package com.flower8.service.impl;

import com.flower8.dao.MessageDao;
import com.flower8.dao.UserDao;
import com.flower8.dto.ActiveCrowdfundDto;
import com.flower8.dto.UserCancelationApplicationDto;
import com.flower8.dto.UserCreditPointDto;
import com.flower8.dto.UserDto;
import com.flower8.dto.UserPointsDto;
import com.flower8.model.User;
import com.flower8.service.SysLogService;
import com.flower8.service.TokenManager;
import com.flower8.service.UserService;
import com.flower8.sysconfig.constants.SysConstants;
import com.flower8.utils.DateUtils;
import com.flower8.utils.PageData;
import com.flower8.utils.SpringUtil;
import com.flower8.utils.StringUtil;
import com.flower8.utils.UserUtil;
import com.flower8.utils.ftpUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户信息service实现类
 * @Author: tanglin
 * @Date: 2019/09/09 17:24
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private MessageDao messageDao;

    /**
     * 获取用户列表
     * @param pd
     * @return
     */
    @Override
    public List<UserDto> getUserList(PageData pd) {
        return userDao.getUserList(pd);
    }

    /**
     * 获取用户数量
     * @param pd
     * @return
     */
    @Override
    public int getUserListCount(PageData pd) {
        return userDao.getUserListCount(pd);
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserDto getUserById(long id) {
        return userDao.getUserById(id);
    }

    /**
     * 根据用户id删除用户
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void deleteById(long id) throws Exception {
        userDao.deleteById(id);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "删除id为"+id+"的用户", UserUtil.getCurrentUser().getAccount()+"删除用户");
    }

    /**
     * 根据用户id更新用用户信息
     * @param user
     * @throws Exception
     */
    @Override
    @Transactional
    public void updateUserById(User user) throws Exception {
        userDao.updateUserById(user);
        SpringUtil.getBean(SysLogService.class).save(UserUtil.getCurrentUser().getId(), "修改用户信息", UserUtil.getCurrentUser().getAccount()+"修改id为："+user.getId()+"用户信息");
    }

    /**
     * 查询用户积分记录
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<UserPointsDto> getUserPoints(PageData pd) throws Exception {
        return userDao.getUserPoints(pd);
    }

    /**
     * 查询用户积分记录总数
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getUserPointsCount(PageData pd) throws Exception {
        return userDao.getUserPointsCount(pd);
    }

    /**
     * 查询用户总积分
     * @param pd
     * @return
     */
    @Override
    public int getTotPointByUser(PageData pd) {
        return userDao.getTotPointByUser(pd);
    }

    /**
     * 查询用户信用分记录
     *
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<UserCreditPointDto> getUserCreditPoints(PageData pd) throws Exception {
        return userDao.getUserCreditPoints(pd);
    }

    /**
     * 查询用户信用分记录总数
     *
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getUserCreditPointsCount(PageData pd) throws Exception {
        return userDao.getUserCreditPointsCount(pd);
    }

    /**
     * 查询用户总信用分
     *
     * @param pd
     * @return
     */
    @Override
    public int getTotalCreditPoint(PageData pd) {
        return userDao.getTotalCreditPoint(pd);
    }

    /**
     * 查询需要人工审核的用户
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<UserDto> getNotPassUser(PageData pd) throws Exception {
        return userDao.getNotPassUser(pd);
    }

    /**
     * 查询需要人工审核的用户总数
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public long getNotPassUserCount(PageData pd) throws Exception {
        return userDao.getNotPassUserCount(pd);
    }

    /**
     * 审核用户实名认证信息
     * @param pd
     * @throws Exception
     */
    @Override
    public void checkUser(PageData pd) throws Exception {
        userDao.checkUser(pd);
    }

    /**
     * 获取用户注销申请列表
     * @param pd
     * @return
     */
    @Override
    public List<UserCancelationApplicationDto> getUserCancelationApplicationList(PageData pd) throws Exception {
        return userDao.getUserCancelationApplicationList(pd);
    }

    /**
     * 获取用户注销申请数量
     * @param pd
     * @return
     */
    @Override
    public int getUserCancelationApplicationListCount(PageData pd) throws Exception {
        return userDao.getUserCancelationApplicationListCount(pd);
    }

    /**
     * 受理用户注销申请
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void acceptCancelationApplication(long id) throws Exception {
        Map param = new HashMap();
        param.put("id", id);
        param.put("status", 1);
        userDao.updateCancelationApplicationStatus(param);
        Map res = userDao.getUserCancelationApplicationById(id);
        int status = (int) res.get("status");
        if (status == 1) {
            int uid = (int) res.get("uid");
            UserDto userDto = userDao.getUserById(uid);
            Timestamp createTime = DateUtils.getCurrentTimestamp();
            Timestamp invalidTime = DateUtils.addMonth(createTime, 6);
            Map paramUserCancelled = new HashMap();
            paramUserCancelled.put("mobile", userDto.getMobile());
            paramUserCancelled.put("createTime", createTime);
            paramUserCancelled.put("invalidTime", invalidTime);
            Map userCancelledRes = userDao.getUserCancelledByMobile(paramUserCancelled);
            if (userCancelledRes != null && !userCancelledRes.isEmpty()) {
                //更新已注销用户表
                userDao.updateUserCancelled(paramUserCancelled);
            } else {
                //添加已注销用户表
                userDao.addUserCancelled(paramUserCancelled);
            }
            //删除用户微信
            paramUserCancelled.put("uid", uid);
            userDao.deleteUserWxForCancelation(paramUserCancelled);
            //更新银行卡信息
            userDao.updateUserCardForCancelation(paramUserCancelled);
            //更新用户表
            Map paramUpdateUser = new HashMap();
            paramUpdateUser.put("id", uid);
            userDao.updateUserForCancelation(paramUpdateUser);
            TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
            tokenManager.deleteServerToken(String.valueOf(uid));
            if (!StringUtil.isEmptyAfterTrim(userDto.getIdCardOnpic())) {
                ftpUtil.delFile(userDto.getIdCardOnpic(), 11);
            }
            if (!StringUtil.isEmptyAfterTrim(userDto.getIdCardOffpic())) {
                ftpUtil.delFile(userDto.getIdCardOffpic(), 11);
            }
        } else {
            throw new Exception();
        }
    }

    /**
     * 驳回用户注销申请
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void rejectCancelationApplication(long id) throws Exception {
        Map param = new HashMap();
        param.put("id", id);
        param.put("status", 2);
        userDao.updateCancelationApplicationStatus(param);
        Map res = userDao.getUserCancelationApplicationById(id);
        int status = (int) res.get("status");
        if (status != 2) {
            throw new Exception();
        }
        int uid = (int) res.get("uid");
        //发送一条驳回注销申请消息
        Map msgParam = new HashMap();
        msgParam.put("uid", uid);
        msgParam.put("type", 1);
        msgParam.put("category", 8);
        msgParam.put("icon", SysConstants.MSG1);
        String msg = "您的的注销申请已被驳回，如有疑问请联系客服。";
        msgParam.put("contents", msg);
        messageDao.addMsg(msgParam);
    }

    /**
     * 查询用户总可用额度
     *
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public Map getTotalUsableAmt(PageData pd) throws Exception {
        return userDao.getTotalUsableAmt(pd);
    }

    /**
     * 查询用户可用额度记录
     * 
     * @param pd
     * @return
     * @throws Exception
     */
	@Override
	public List<Map> getUserUsableAmtList(PageData pd) throws Exception {
		return userDao.getUserUsableAmtList(pd);
	}

    /**
     * 查询用户可用额度记录总数
     *
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getUserUsableAmtCount(PageData pd) throws Exception {
        return userDao.getUserUsableAmtCount(pd);
    }

}
